package com.hstcs.assets;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;

public class Assets {
	public static AssetManager assetManager;
	public static Skin skin = null;
	public static ObjectMap<String, ParticleEffectPool> pools;
	public static Array<TextureRegion> mao_regions = null;
	public static Array<TextureRegion> mao_regions_flip = null;
	public static Array<TextureRegion> gou_regions = null;
	public static Array<TextureRegion> gou_regions_flip = null;
	public static ObjectMap<TextureRegion, TextureRegion> flipMap = 
			new ObjectMap<TextureRegion,TextureRegion>();

	public static void load() {
		FileName.init();
		Area.init();
		assetManager = new AssetManager();
		assetManager.load(FileName._SKIN, Skin.class);
		assetManager.load(FileName._BG_START, Texture.class);
		assetManager.load(FileName._BG_GAME, Texture.class);
		assetManager.load(FileName._AN_GOU, Texture.class);
		assetManager.load(FileName._AN_MAO, Texture.class);
	}

	public static void loadfinish() {
		skin = get(FileName._SKIN);
	}

	public static <T> T get(String fileName) {
		return assetManager.get(fileName);
	}

	public static <T> T get(String fileName, Class<T> type) {
		return assetManager.get(fileName, type);
	}
	
	public static Texture getTexture(String fileName){
		Texture texture = (Texture)assetManager.get(fileName);
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		return texture;
	}
	
	
	public  static TextureRegion getRegion(String str){
		return new TextureRegion(getTexture(str));
	}
	
	public static TextureRegionDrawable getDrawable(String str){
		return new TextureRegionDrawable(getRegion(str));
	}
	
	public static TextureRegion[] getRegions(String str, int n){
		TextureRegion region = getRegion(str);
		return region.split(region.getRegionWidth()/n, region.getRegionHeight())[0];
	}
	
	public static TextureRegion[][] getRegions(String str, int n, int m){
		TextureRegion region = getRegion(str);
		return region.split(region.getRegionWidth()/n, region.getRegionHeight()/m);
	}
	//t,k 从0开始
	public static Array<TextureRegion> getRegionArray(String str, int n, int m, int t, int k){
	
		Array<TextureRegion> regionArray = new Array<TextureRegion>();
		TextureRegion[][] regions = getRegions(str, n, m);
		for(int i = 0 ; i < m  ; i++)
		{
			for(int j = 0; j < n  ;j++)
			{
				if(i * n + j < t)
				{
					continue;
				}else if((i * n + j) >= t &&(i * n + j)<= k)
				{
					regionArray.add(regions[i][j]);
					if((i * n + j)== k) return regionArray;
				}
			}
		}
		System.out.println("error ");
		return null;
	}
	
	public static Array<TextureRegion> getMg(int type){
		if(type == Common.TYPE_MAO){
			if(mao_regions == null){
				mao_regions = new Array<TextureRegion>();
				mao_regions.add(skin.getRegion("mao1"));
				TextureRegion flipmao1 = new TextureRegion(skin.getRegion("mao1"));
				flipmao1.flip(true, false);
				flipMap.put(skin.getRegion("mao1"), flipmao1);
				mao_regions.add(skin.getRegion("mao2"));
				TextureRegion flipmao2 = new TextureRegion(skin.getRegion("mao2"));
				flipmao2.flip(true, false);
				flipMap.put(skin.getRegion("mao2"), flipmao1);
				mao_regions.add(skin.getRegion("mao3"));
				TextureRegion flipmao3 = new TextureRegion(skin.getRegion("mao3"));
				flipmao3.flip(true, false);
				flipMap.put(skin.getRegion("mao3"), flipmao3);
				mao_regions.add(skin.getRegion("mao4"));
				TextureRegion flipmao4 = new TextureRegion(skin.getRegion("mao4"));
				flipmao4.flip(true, false);
				flipMap.put(skin.getRegion("mao4"), flipmao4);
			}
			return mao_regions;
			
		}else if(type == Common.TYPE_GOU){
			if(gou_regions == null){
				gou_regions = new Array<TextureRegion>();
				gou_regions.add(skin.getRegion("gou1"));
				TextureRegion flipGou1 = new TextureRegion(skin.getRegion("gou1"));
				flipGou1.flip(true, false);
				flipMap.put(skin.getRegion("gou1"), flipGou1);
				gou_regions.add(skin.getRegion("gou2"));
				TextureRegion flipGou2 = new TextureRegion(skin.getRegion("gou2"));
				flipGou2.flip(true, false);
				flipMap.put(skin.getRegion("gou2"), flipGou2);
				gou_regions.add(skin.getRegion("gou3"));
				TextureRegion flipGou3 = new TextureRegion(skin.getRegion("gou3"));
				flipGou3.flip(true, false);
				flipMap.put(skin.getRegion("gou3"), flipGou3);
				gou_regions.add(skin.getRegion("gou2"));
			}
			return gou_regions;
		}
		return null;
	}
	public static Array<TextureRegion> getMgFlip(int type){
		if(type == Common.TYPE_MAO){
			if (null == mao_regions_flip) {
				Array<TextureRegion> regions = getMg(type);
				for (TextureRegion region : regions) {
					TextureRegion re = new TextureRegion(region);
					re.flip(true, false);
					mao_regions_flip.add(re);
				}
			}
			return mao_regions_flip;

		}else if(type == Common.TYPE_GOU){
			if (null == gou_regions_flip) {
				Array<TextureRegion> regions = getMg(type);
				for (TextureRegion region : regions) {
					TextureRegion re = new TextureRegion(region);
					re.flip(true, false);
					gou_regions_flip.add(re);
				}
			}
			return gou_regions_flip;
		}
		return null;
	}
}
